//
//  MBProgressHUD+GM.m
//  Teecloud
//
//  Created by 小熊 on 2018/7/9.
//  Copyright © 2018年 小熊. All rights reserved.
//

#import "MBProgressHUD+GM.h"

@implementation MBProgressHUD (GM)
/**
 *  HUD显示在指定view上
 *
 *  @param title   显示的文字
 *  @param view   指定的view
 */
+ (void)showGifAnimation:(NSString *)title toView:(UIView *)view
{
    if (!view){
        view = [[UIApplication sharedApplication].windows lastObject];
    }
    //    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:image withExtension:@"gif"];//加载GIF图片
    //    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);//将GIF图片转换成对应的图片源
    //    size_t frameCout=CGImageSourceGetCount(gifSource);//获取其中图片源个数，即由多少帧图片组成
    //    NSMutableArray* frames=[[NSMutableArray alloc] init];//定义数组存储拆分出来的图片
    //    for (size_t i=0; i < frameCout; i++){
    //         CGImageRef imageRef=CGImageSourceCreateImageAtIndex(gifSource, i, NULL);//从GIF图片中取出源图片
    //         UIImage* imageName=[UIImage imageWithCGImage:imageRef];//将图片源转换成UIimageView能使用的图片源
    //         [frames addObject:imageName];//将图片加入数组中
    //         CGImageRelease(imageRef);
    //    }
    
    NSMutableArray* frames = [NSMutableArray array];
    for (NSUInteger i = 0; i<=50; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"ani_%ld", i]];
        [frames addObject:image];
    }
    UIImageView* imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    imageview.center = view.center;
    imageview.animationImages = frames;
    imageview.animationDuration = 2;
    [imageview startAnimating];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.style=MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.alpha=0.0;
    //    hud.bezelView.backgroundColor = kNormalThemeColor;
    //    hud.bezelView.color = kNormalThemeColor;
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = imageview;
    //    hud.label.text = title;
    hud.label.textColor = kDarkGrayColor;
    //设置中间显示区域的透明度为完全透明
    //    hud.opacity = 0.0;
    hud.animationType = MBProgressHUDAnimationZoomIn;
    hud.bezelView.backgroundColor=[UIColor clearColor];
    //显示
    [hud showAnimated:YES];
    
}
/**
 *  HUD显示在Window上
 */
+ (void)showGifAnimationToWindow:(NSString *)title;
{
    [self showGifAnimation:title toView:nil];
}
/**HUD从指定的view上移除*/
+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil){
        view = [[UIApplication sharedApplication].windows lastObject];
    }
    [self hideHUDForView:view animated:YES];
}
/**HUD从window上移除*/
+ (void)hideHUD
{
    [self hideHUDForView:nil];
}

/**
 *  HUD在指定view上显示成功或失败的提示提示文字
 *
 *  @param message   显示的文字
 *  @param view   指定的view
 */
+ (void)showErrorOrSuc:(NSString *)message toView:(UIView *)view
{
    if (!view)
    {
        view = [[UIApplication sharedApplication].windows lastObject];
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message;
    hud.label.numberOfLines = 3;
    hud.mode = MBProgressHUDModeCustomView;
    hud.label.textColor = kWhiteColor;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = kBlackColor;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:true afterDelay:1.5];
}

/**
 *  HUD在Window上显示成功或失败的提示提示文字
 *
 *  @param message   显示的文字
 */
+ (void)showErrorOrSucToWindow:(NSString *)message
{
    [self showErrorOrSuc:message toView:nil];
}
@end
